<?php

namespace App\Http\Controllers;

use App\Models\Notice;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Libs\Helper_Huanxin;
use App\Models\User;
use App\Http\Requests\NoticeRequest;

class NoticeController extends CommonController
{

    /**
     * 获取通知列表
     */
    public function getIndex()
    {
        $lists = Notice::orderBy('id', 'asc')->with('user')->paginate(10);

//        dd($lists);

        return view('notice.index')->with('lists', $lists)->with('actions', $this->actions);
    }


    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = Notice::orderBy('id', 'asc')->with('user');

        if (isset($data['title']) && $data['title'] != '') {
            $sql->where('title', 'like', '%' . trim($data['title']) . '%');
        }

        if (isset($data['nickname']) && $data['nickname'] != '') {
            $uids = User::where('realname', 'like', '%' . trim($data['nickname']) . '%')->orWhere('nickname', 'like', '%' . trim($data['nickname']) . '%')->lists('uid');
            $sql->whereIn('uid', $uids);
        }


        if (isset($data['type']) && $data['type'] != '') {
            $sql->where('type', $data['type']);
        }

        $lists = $sql->paginate(10);

        return view('notice.search')->with("lists", $lists)->with("title", $data['title'])
            ->with("nickname", $data['nickname'])->with("type", $data['type'])->with("actions", $this->actions);

    }

    /**
     * 获取新建一条通知页面
     */
    public function getCreate()
    {
        return view('notice.create');
    }

    /**
     * 执行一条创建
     */
    public function postCreate(NoticeRequest $request)
    {
        $data = $request->input();
        if ($data['receiver'] == 'single') {
            unset($data['formType'], $data['_token'], $data['receiver']);
            Notice::create($data);
           // @Helper_Huanxin::sendMessage(array($data['username']), $data['content'], "admin");
        } else {
//            dd(2);
            $username = User::orderBy('uid', 'asc')->lists('username');
            //给所有的用户发送信息 先注释
//            @Helper_Huanxin::sendMessage($username, $data['content'],"admin");
        }

        return AjaxCallbackMessage('恭喜信息发送成功', true, url('notice/index'));
    }
}
